<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    
</head>
<script type="text/javascript">
	$(function (){
		$("#parent").val(0)
		findall()
		nextfind()
	})
	function agin(id){
		$("#parent").val(id)
		findall()
		nextfind()

	}

	//拼接展示页面
	function findall(){
		$.ajax({
			url:"/itemCatController/findAll?id=" + $("#parent").val(),
			type: "post",
			dataType: "json",
			async: false,
			success: function (list){
				var itemCat = list.t;
				var html = ""
				for (var i = 0; i < itemCat.length; i++) {
					$("#parent").val(itemCat[i].parentId)
					html +=	'<tr><td><input type = "hidden" id="itemcateid" value="' + itemCat[i].id + '" > <input type = "checkbox" name="check" value="' + itemCat[i].id + '"> </td><td>' + itemCat[i].id + '</td>'
					html += '<td>' + itemCat[i].name + '</td>'
					html += '<td>' + itemCat[i].typeId + '</td>'
					html += '<td className="text-center">'
					html +=	'<button type="button" onclick="agin(' + itemCat[i].id + ')" className="btn bg-olive btn-xs">查询下级</button>'
					html +=	'<button type="button" onclick="echo(' + itemCat[i].id + ')" className="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal2">修改</button></td></tr>'
				}

				$("#itemcate").html(html)
			},
			error: function (list){
				console.log(list)
				alert("网络错误")
			}
		})
	}

	//上方a标签
	function nextfind() {
		$.ajax({
			url: "/itemCatController/returnfindAll?parent=" + $("#parent").val(),
			type: "post",
			dataType: "json",
			async: false,
			success: function (list) {
				var tbItemCat = list.t;
				// console.log(tbItemCat)
				var as = "<li><a href=\"#\" onClick=\"agin(0)\">顶级分类列表</a></li>"
				var text = "顶级分类列表"
				if (tbItemCat != null){
					for (var i = 0; i < tbItemCat.length; i++) {
						as += '<li><a href="#" onclick="agin(' + tbItemCat[i].id + ')" >' + tbItemCat[i].name + '</a></li>'
						text += ">>>" + tbItemCat[i].name
					}
				}
				$("#id_Text").text(text)
				$("#id_Text2").text(text)
				$("#aid").html(as);
			},
			error: function (list) {
				console.log(list)
				alert("网络错误")
			}
		})

	}

	//增加
	function insert(){
		$.ajax({
			url: "/itemCatController/insertItemCat",
			type: "post",
			dataType: "json",
			data: {id:null,parentId:$("#parent").val(),name:$("#name").val(),typeId:$("#typeId").val()},
			async: false,
			success: function (list) {
				console.log(list)
				agin($("#parent").val())
				alert(list.codename)
			},
			error: function (list) {
				console.log(list)
				alert("网络错误")
			}
		})
	}

	//回显
	function echo(id){
		$.ajax({
			url: "/itemCatController/echoItemCat?id=" + id,
			type: "post",
			dataType: "json",
			async: false,
			success: function (list) {
				console.log(list)
				var itemCat = list.t;
				$("#name2").val(itemCat.name)
				$("#typeId2").val(itemCat.typeId)

			},
			error: function (list) {
				console.log(list)
				alert("网络错误")
			}
		})
	}

	//修改
	function update(id){
		$.ajax({
			url: "/itemCatController/updateItemCat",
			type: "post",
			dataType: "json",
			data: {id:$("#itemcateid").val(),parentId:$("#parent").val(),name:$("#name2").val(),typeId:$("#typeId2").val()},
			async: false,
			success: function (list) {
				console.log(list)
				agin($("#parent").val())
				alert(list.codename)
			},
			error: function (list) {
				console.log(list)
				alert("网络错误")
			}
		})
	}

	//删除
	function deleteById(){
		var checks = document.getElementsByName("check");
		var ids = []
		for (var i = 0; i < checks.length; i++) {
			if (checks[i].checked){
				ids.push(checks[i].value)
			}
		}
		$.ajax({
			url: "/itemCatController/deleteById?ids=" + ids,
			type: "post",
			dataType: "json",
			async: false,
			success: function (list) {
				console.log(list)
				agin($("#parent").val())
				alert(list.codename)
			},
			error: function (list) {
				console.log(list)
				alert("网络错误")
			}
		})
	}

</script>
<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                	<input type="hidden" id="parent" value="0">
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" id="aid">

	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" onclick="deleteById()" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="itemcate">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
              
                                
<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td id="id_Text">
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input id="name" class="form-control" placeholder="商品分类名称">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>	      		
		      			<input id="typeId" select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button onclick="insert()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 修改窗口 -->
  <div class="modal fade" id="editModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel2">商品分类编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>上级商品分类</td>
						  <td id="id_Text2">
						  </td>
					  </tr>
					  <tr>
						  <td>商品分类名称</td>
						  <td><input id="name2" class="form-control" placeholder="商品分类名称">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td>
							  <input id="typeId2" select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button onclick="update()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
</html>